package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.NoticeListAdapter;
import com.afd.ozblip.adapter.UserMsgListAdapter;
import com.afd.ozblip.entity.NoticeEntity;
import com.afd.ozblip.entity.UserEntity;
import com.afd.ozblip.entity.UserMsgEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class UserMsgListActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private ListView userMsgLv;
	private ListView userNoticeLv;
	private RadioGroup userMsgRg;
	private RelativeLayout userMsgRl;
	private RelativeLayout userNoticeRl;
	private TextView userMsgTipTv;
	private TextView userNoticeTipTv;

	/****************** 数据变量 **************************/
	private UserMsgListAdapter userMsgAdapter;
	private List<UserMsgEntity> userMsgList;
	private NoticeListAdapter userNoticeAdapter;
	private List<NoticeEntity> userNoticeList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_message_list);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 获取数据
		requestUserMsgListData();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.userMsgLv = (ListView) findViewById(R.id.user_msg_list_lv);
		this.userNoticeLv = (ListView) findViewById(R.id.user_notice_list_lv);
		this.userMsgRg = (RadioGroup) findViewById(R.id.user_msg_tab_rg);
		this.userMsgRl = (RelativeLayout) findViewById(R.id.user_msg_message_rl);
		this.userNoticeRl = (RelativeLayout) findViewById(R.id.user_msg_notice_rl);
		this.userMsgTipTv = (TextView) findViewById(R.id.user_msg_list_tip_tv);
		this.userNoticeTipTv = (TextView) findViewById(R.id.user_notice_list_tip_tv);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		this.userMsgRg
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						switch (checkedId) {
						case R.id.user_msg_message_rb:
							userMsgRl.setVisibility(0);
							userNoticeRl.setVisibility(8);
							requestUserMsgListData();
							break;
						case R.id.user_msg_notice_rb:
							userMsgRl.setVisibility(8);
							userNoticeRl.setVisibility(0);
							requestUserNoticeListData();
							break;

						default:
							break;
						}
					}
				});
		userMsgLv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				UserMsgEntity userMsg = (UserMsgEntity) userMsgAdapter
						.getItem(position);
				Intent chatIntent = new Intent();
				chatIntent.setClass(UserMsgListActivity.this,
						UserChatActivity.class);
				UserEntity friend = new UserEntity();
				friend.setUserName(userMsg.getUserName());
				friend.setUserId(userMsg.getUserName());
				chatIntent.putExtra("friendInfo", friend);
				startActivity(chatIntent);
			}
		});
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		// Intent intent = getIntent();
	}

	/**
	 * 获取用户签到详情数据
	 */
	private void requestUserMsgListData() {
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
						nearbyBusinessParamMap.put("userId",
								commonUserInfo.getUserId());
						nearbyBusinessParamMap.put("pageCnt", 1);
						nearbyBusinessParamMap.put("pageRow", 20);
						// 发送请求
						byte[] bMsgList = HttpUtil.requestByPost(
								UserMsgListActivity.this,
								Constants.USER_MSG_API, nearbyBusinessParamMap);
						if (bMsgList != null) {
							String jsonStr = new String(bMsgList, "UTF-8");
							Map<String, Object> test = JSON.parseObject(
									jsonStr,
									new TypeReference<Map<String, Object>>() {
									});
							userMsgList = JSON.parseArray(test.get("data")
									.toString(), UserMsgEntity.class);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						setMsgListAdapter();
					}

				});
		ald.execute(0);
	}

	/**
	 * 调用消息列表适配器
	 */
	public void setMsgListAdapter() {
		if (CollectionUtil.UnNullAndZero(userMsgList)) {
			this.userMsgTipTv.setVisibility(8);
			this.userMsgAdapter = new UserMsgListAdapter(
					UserMsgListActivity.this);
			this.userMsgAdapter.addItems(userMsgList);
			this.userMsgLv.setAdapter(this.userMsgAdapter);
		} else {
			this.userMsgTipTv.setVisibility(0);
			this.userMsgTipTv.setText(getResources().getString(
					R.string.no_message_tip));
		}
	}

	/**
	 * 获取用户通告列表数据
	 */
	private void requestUserNoticeListData() {
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
						nearbyBusinessParamMap.put("userId",
								commonUserInfo.getUserId());
						nearbyBusinessParamMap.put("pageCnt", 1);
						nearbyBusinessParamMap.put("pageRow", 20);
						// 发送请求
						byte[] bMsgList = HttpUtil.requestByPost(
								UserMsgListActivity.this,
								Constants.USER_NOTICE_API,
								nearbyBusinessParamMap);

						if (bMsgList != null) {
							String jsonStr = new String(bMsgList, "UTF-8");
							Map<String, Object> test = JSON.parseObject(
									jsonStr,
									new TypeReference<Map<String, Object>>() {
									});
							userNoticeList = JSON.parseArray(
									test.get("resultList").toString(),
									NoticeEntity.class);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						setNoticeListAdapter();
					}

				});
		ald.execute(0);
	}

	/**
	 * 调用通知列表适配器
	 */
	public void setNoticeListAdapter() {
		if (CollectionUtil.UnNullAndZero(userNoticeList)) {
			this.userNoticeTipTv.setVisibility(8);
			this.userNoticeAdapter = new NoticeListAdapter(
					UserMsgListActivity.this);
			this.userNoticeAdapter.addItems(userNoticeList);
			this.userNoticeLv.setAdapter(this.userNoticeAdapter);
		} else {
			this.userNoticeTipTv.setVisibility(0);
			this.userNoticeTipTv.setText(getResources().getString(
					R.string.no_notice_tip));
		}
	}
}